using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace ArchSoft
{
    public partial class ScheduleInfA : Form
    {
        private string _folderPath;
        DataTable reportDT = new DataTable("Schedule");
        DataRow row;
        DataColumn col;

        string _name = "";

        public string FolderPath
        {
            set { _folderPath = value; }
        }

        public ScheduleInfA()
        {
            InitializeComponent();

            //Initilize column to DataTable

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Element";
            col.Caption = "Element";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Drawing";
            col.Caption = "Drawing";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Date_Start";
            col.Caption = "Date Start";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Date_End";
            col.Caption = "Date End";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Finish";
            col.Caption = "Finish";
            reportDT.Columns.Add(col);


        }

        public void setLoad()
        { 
            string[] dir = Directory.GetDirectories(_folderPath);

            foreach (string fol in dir)
            {
                if (Path.GetFileNameWithoutExtension(fol).IndexOf("Xref") < 0)
                { 
                    foreach (string file in Directory.GetFiles(fol))
                    {
                        if (Path.GetExtension(file).ToLower() != "dwg")
                        {
                            row = reportDT.NewRow();
                            row["Element"] = Path.GetFileNameWithoutExtension(fol); ;
                            row["Drawing"] = Path.GetFileName(file);
                            row["Date_Start"] = DateTime.Now.ToShortDateString();
                            row["Date_End"] = DateTime.Now.ToShortDateString();
                            row["Finish"] = "Yes";
                            reportDT.Rows.Add(row);
                        }
                    } 
                }
            }
        }

        private void ScheduleInfA_Load(object sender, EventArgs e)
        {
            try
            {
                //string[] dir = Directory.GetDirectories(_folderPath);

                //foreach (string fol in dir)
                //{
                //    if (Path.GetFileNameWithoutExtension(fol).IndexOf("Xref") > 0)
                //        continue;

                //    foreach (string file in Directory.GetFiles(fol))
                //    {
                //        if (Path.GetExtension(file).ToLower() != "dwg")
                //        {
                //            ListViewItem nit = new ListViewItem();
                //            ListViewItem.ListViewSubItem sit0 = new ListViewItem.ListViewSubItem();
                //            ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem();
                //            ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem();
                //            ListViewItem.ListViewSubItem sit3 = new ListViewItem.ListViewSubItem();
                //            ListViewItem.ListViewSubItem sit4 = new ListViewItem.ListViewSubItem();

                //            nit.Text = Path.GetFileNameWithoutExtension(fol);
                //            sit0.Text = Path.GetFileName(file);
                //            sit1.Text = DateTime.Now.ToShortDateString();
                //            sit2.Text = DateTime.Now.ToShortDateString();
                //            sit3.Text = "YES";
               
                //            nit.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { sit0, sit1, sit2, sit3});

                //            listView1.Items.Add(nit);
                //        }
                //    }
                //}
                string path = _folderPath + Path.DirectorySeparatorChar + "Schedule.sch";
                if (!File.Exists(path))
                {
                    setLoad();
                    ViewList();
                }
                    DataSet ds = new DataSet();
                    ds.ReadXml(path);
                    reportDT = ds.Tables[0].Copy();
                    ViewList();    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void ViewList()
        {
            
            for (int i = 0; i < reportDT.Rows.Count; i++)
            {
                ListViewItem nit = new ListViewItem();
                ListViewItem.ListViewSubItem sit0 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit3 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit4 = new ListViewItem.ListViewSubItem();

                nit.Text = reportDT.Rows[i]["Element"].ToString();
                sit0.Text = reportDT.Rows[i]["Drawing"].ToString();
                sit1.Text = reportDT.Rows[i]["Date_Start"].ToString();
                sit2.Text = reportDT.Rows[i]["Date_End"].ToString();
                sit3.Text = reportDT.Rows[i]["Finish"].ToString();

                nit.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { sit0, sit1, sit2, sit3 });
                listView1.Items.Add(nit);
            }

           
        }

        void ClearList()
        {
            listView1.Items.Clear();
        }
        int _index;

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count == 0)
                return;

           
                ListView.SelectedListViewItemCollection temp = this.listView1.SelectedItems;

                foreach (ListViewItem item in temp)
                {
                    _name  = item.SubItems[1].Text;
                    _index = item.Index;
                }
                
                Process();


            
            //SetScheduleInf frm = new SetScheduleInf();
            //frm.StartPosition = FormStartPosition.CenterScreen;
            ////if (frm.ShowDialog() == DialogResult.Yes)
            ////{
            ////    for (int i = 0; i < reportDT.Rows.Count; i++)
            ////    {
            ////        if (Name.Trim() == reportDT.Rows[i]["Drawing"].ToString().Trim())
            ////        {
            ////            reportDT.Rows[i]["Date_Start"] = frm.StartDate.ToShortDateString();
            ////            reportDT.Rows[i]["Date_End"] = frm.EndDate.ToShortDateString();
            ////            reportDT.Rows[i]["Finish"] = frm.Finish;
            ////        }
            ////    }
            ////}
            //frm.ShowDialog();
            //if (frm.DialogResult == DialogResult.Yes)
            //{
            //    for (int i = 0; i < reportDT.Rows.Count; i++)
            //    {
            //        if (Name.Trim() == reportDT.Rows[i]["Drawing"].ToString().Trim())
            //        {
            //            reportDT.Rows[i]["Date_Start"] = frm.StartDate.ToShortDateString();
            //            reportDT.Rows[i]["Date_End"] = frm.EndDate.ToShortDateString();
            //            reportDT.Rows[i]["Finish"] = frm.Finish;
            //        }
            //    }
            //}
            //ClearList();
            //ViewList();
            //SaveToXML();
        }
        
        public void Process()
        {
            SetScheduleInf frm = new SetScheduleInf();
            //frm.StartPosition = FormStartPosition.CenterScreen;
            //if (frm.ShowDialog() == DialogResult.Yes)
            //{
            //    for (int i = 0; i < reportDT.Rows.Count; i++)
            //    {
            //        if (Name.Trim() == reportDT.Rows[i]["Drawing"].ToString().Trim())
            //        {
            //            reportDT.Rows[i]["Date_Start"] = frm.StartDate.ToShortDateString();
            //            reportDT.Rows[i]["Date_End"] = frm.EndDate.ToShortDateString();
            //            reportDT.Rows[i]["Finish"] = frm.Finish;
            //        }
            //    }
            //}
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.Yes)
            {
                for (int i = 0; i < reportDT.Rows.Count; i++)
                {
                    if (_name.Trim() == reportDT.Rows[i]["Drawing"].ToString().Trim())
                    {
                        reportDT.Rows[i]["Date_Start"] = frm.StartDate.ToShortDateString();
                        reportDT.Rows[i]["Date_End"] = frm.EndDate.ToShortDateString();
                        reportDT.Rows[i]["Finish"] = frm.Finish;
                    }
                }
            }
            listView1.Items.Clear();

            for (int i = 0; i < reportDT.Rows.Count; i++)
            {
                ListViewItem nit = new ListViewItem();
                ListViewItem.ListViewSubItem sit0 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit1 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit2 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit3 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem sit4 = new ListViewItem.ListViewSubItem();

                nit.Text = reportDT.Rows[i]["Element"].ToString();
                sit0.Text = reportDT.Rows[i]["Drawing"].ToString();
                sit1.Text = reportDT.Rows[i]["Date_Start"].ToString();
                sit2.Text = reportDT.Rows[i]["Date_End"].ToString();
                sit3.Text = reportDT.Rows[i]["Finish"].ToString();

                nit.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { sit0, sit1, sit2, sit3 });
                listView1.Items.Add(nit);
            }
            //ClearList();
           // ViewList();
        }

        public void SaveToXML()
        {
            string path = _folderPath + Path.DirectorySeparatorChar + "Schedule.sch";
            DataSet ds = new DataSet();
            ds.Tables.Clear();
            ds.Tables.Add(reportDT);
            ds.WriteXml(path);

            //ClearList();
            //ScheduleInfA_Load(null, EventArgs.Empty);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveToXML();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Process();
        }
    }
}